/*
 * Copyright (C) 2008 Matt Falkoski
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.falko.android.raven.settings;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.falko.android.raven.R;

public class SeekBarPreference extends Preference implements
		OnSeekBarChangeListener {

	private final String TAG = getClass().getName();

	private static final String NAMESPACE = "http://schemas.android.com/apk/res/android";
	private static final int DEFAULT_VALUE = 5;

	private int max_ = 100;
	private int min_ = 1;
	private int interval_ = 1;
	private int value_;
	private SeekBar seekBar_;

	private TextView mStatusText;

	public SeekBarPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		initPreference(context, attrs);
	}

	public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initPreference(context, attrs);
	}

	private void initPreference(Context context, AttributeSet attrs) {
		max_ = attrs.getAttributeIntValue(NAMESPACE, "max", 100);
		min_ = attrs.getAttributeIntValue(NAMESPACE, "min", 1);
		interval_ = attrs.getAttributeIntValue(NAMESPACE, "interval", 1);
	}

	@Override
	protected View onCreateView(ViewGroup parent) {

		RelativeLayout layout = null;

		try {
			LayoutInflater mInflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			layout = (RelativeLayout) mInflater.inflate(
					R.layout.seek_bar_preference, parent, false);
		} catch (Exception e) {
			Log.e(TAG, "Error creating seek bar preference", e);
		}

		seekBar_ = (SeekBar) layout.findViewById(R.id.seekBar);
		seekBar_.setMax(max_ - min_);
		seekBar_.setOnSeekBarChangeListener(this);

		return layout;
	}

	@Override
	public void onBindView(View view) {
		super.onBindView(view);
		mStatusText = (TextView) view.findViewById(R.id.seekBarPrefValue);
		mStatusText.setText(String.valueOf(value_));
		mStatusText.setMinimumWidth(30);
		seekBar_.setProgress(value_ - min_);
	}

	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		int newValue = progress + min_;

		if (newValue > max_)
			newValue = max_;
		else if (newValue < min_)
			newValue = min_;
		else if (interval_ != 1 && newValue % interval_ != 0)
			newValue = Math.round(((float) newValue) / interval_) * interval_;

		if (!callChangeListener(newValue)) {
			seekBar.setProgress(value_ - min_);
			return;
		}

		value_ = newValue;
		mStatusText.setText(String.valueOf(newValue));
		persistInt(newValue);

	}

	public void onStartTrackingTouch(SeekBar seekBar) {
	}

	public void onStopTrackingTouch(SeekBar seekBar) {
		notifyChanged();
	}

	@Override
	protected Object onGetDefaultValue(TypedArray ta, int index) {

		return ta.getInt(index, DEFAULT_VALUE);
	}

	@Override
	protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {

		if (restoreValue) {
			value_ = getPersistedInt(value_);
		} else {
			int temp = 0;
			try {
				temp = (Integer) defaultValue;
			} catch (Exception ex) {
				Log.e(TAG, "Invalid default value: " + defaultValue.toString());
			}

			persistInt(temp);
			value_ = temp;
		}

	}

}
